﻿using System;
using System.IO;
using System.Reflection;

class PetResource_82
{
    public Int32 id { get; set; }
    private Int32 epic_82_column_1 { get; set; }
    public Int32 type { get; set; }
    private Int32 epic_82_column_2 { get; set; }
    public Int32 name_id { get; set; }
    public Int32 cage_id { get; set; }
    public Int32 attribute_flag { get; set; }
    public Single size { get; set; }
    public Single scale { get; set; }
    public Single target_fx_size { get; set; }
    public Int32 walk_type { get; set; }
    public Int32 slant_type { get; set; }
    public Int32 camera_x { get; set; }
    public Int32 camera_y { get; set; }
    public Int32 camera_z { get; set; }
    public Single target_x { get; set; }
    public Single target_y { get; set; }
    public Single target_z { get; set; }
    public String model { get; set; }
    public Int32 motion_file_id { get; set; }
    public Int32 texture_group { get; set; }
    public Int32 local_flag { get; set; }

    public void ReadFile(BinaryReader br)
    {
        this.id = br.ReadInt32();
        this.epic_82_column_1 = br.ReadInt32();
        this.type = br.ReadInt32();
        this.epic_82_column_2 = br.ReadInt32();
        this.name_id = br.ReadInt32();
        this.cage_id = br.ReadInt32();
        this.attribute_flag = br.ReadInt32();
        this.size = br.ReadSingle();
        this.scale = br.ReadSingle();
        this.target_fx_size = br.ReadSingle();
        this.walk_type = br.ReadInt32();
        this.slant_type = br.ReadInt32();
        this.camera_x = br.ReadInt32();
        this.camera_y = br.ReadInt32();
        this.camera_z = br.ReadInt32();
        this.target_x = br.ReadSingle();
        this.target_y = br.ReadSingle();
        this.target_z = br.ReadSingle();
        this.model = SFM.BytesToString(br.ReadBytes(256));
        this.motion_file_id = br.ReadInt32();
        this.texture_group = br.ReadInt32();
        this.local_flag = br.ReadInt32();
    }

    public void SaveFile(BinaryWriter bw)
    {
        bw.Write(this.id);
        bw.Write(this.epic_82_column_1);
        bw.Write(this.type);
        bw.Write(this.epic_82_column_2);
        bw.Write(this.name_id);
        bw.Write(this.cage_id);
        bw.Write(this.attribute_flag);
        bw.Write(this.size);
        bw.Write(this.scale);
        bw.Write(this.target_fx_size);
        bw.Write(this.walk_type);
        bw.Write(this.slant_type);
        bw.Write(this.camera_x);
        bw.Write(this.camera_y);
        bw.Write(this.camera_z);
        bw.Write(this.target_x);
        bw.Write(this.target_y);
        bw.Write(this.target_z);
        bw.Write(SFM.StringToBytes(this.model));
        bw.Write(new Byte[256 - this.model.Length]);
        bw.Write(this.motion_file_id);
        bw.Write(this.texture_group);
        bw.Write(this.local_flag);
    }

    public string ToDatabase()
    {
        return "INSERT INTO [dbo].[PetResource] ([id],[type],[name_id],[cage_id],[rate],[size],[scale],[target_fx_size],[walk_type],[slant_type],[camera_x],[camera_y],[camera_z],[target_x],[target_y],[target_z],[model],[motion_file_id],[texture_group],[local_flag])VALUES(@id,@type,@name_id,@cage_id,@rate,@size,@scale,@target_fx_size,@walk_type,@slant_type,@camera_x,@camera_y,@camera_z,@target_x,@target_y,@target_z,@model,@motion_file_id,@texture_group,@local_flag)";
    }

    public static String FromDatabase()
    {
        String rString = "SELECT ";
        foreach (FieldInfo fieldInfo in typeof(PetResource).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static))
        {
            rString += string.Format("[{0}], ", fieldInfo.Name.Substring(1, fieldInfo.Name.IndexOf('>') - 1));
        }
        return rString.Substring(0, rString.Length - 2) + " FROM PetResource order by id";
    }

    public void FromDatabaseExecute(System.Data.SqlClient.SqlDataReader drExecute)
    {
        foreach (FieldInfo fieldInfo in typeof(PetResource).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static))
        {
            String sTempName = null;
            sTempName = fieldInfo.Name.Substring(1, fieldInfo.Name.IndexOf('>') - 1);

            if (fieldInfo.FieldType == typeof(String))
            {
                fieldInfo.SetValue(this, drExecute[sTempName].ToString());
            }
            else if (fieldInfo.FieldType == typeof(Int16))
            {
                fieldInfo.SetValue(this, Convert.ToInt16(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Int32))
            {
                fieldInfo.SetValue(this, Convert.ToInt32(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Single))
            {
                fieldInfo.SetValue(this, Convert.ToSingle(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Decimal))
            {
                fieldInfo.SetValue(this, Convert.ToDecimal(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Double))
            {
                fieldInfo.SetValue(this, Convert.ToDouble(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Byte))
            {
                fieldInfo.SetValue(this, Convert.ToByte(drExecute[sTempName]));
            }
        }
    }
}
